<?php namespace Install\Controller; 

use Hdphp\Controller\Controller;

//测试控制器
class InstallController extends Controller
{

	public function __init()
    {
        if(is_file('public/lock'))
        {
            View::make('lock');
            exit;
        }
    }
    public function setup()
    {
        View::make();
    }

    public function check()
    {
        $fun['fun']['PDO']=class_exists('Pdo')?true:false;//检测是否开启PDO;
        $fun['fun']['GD']=function_exists('imagecreate')?true:false;//检测时候开启GD库;s
        $fun['fun']['mb_string']=function_exists('mb_substr')?true:false;//检测有没有字符串截取函数;

        $fun['dir']['storage']=is_writable('storage')?'OK':'目录必须有写权限';
        $fun['dir']['backup']=class_exists('Pdo')?'OK':'目录必须有写权限';

        View::with('fun',$fun);
        View::make();
    }
    //配置数据库连接
    public function database()
    {
        if(IS_POST)
        {

            $data=include 'config/database.php';
            $data=array_merge($data,$_POST);

            if(!@mysql_connect($data['host'],$data['user'],$data['password']))
            {
                $this->error('数据库连接失败');
            }
            $sql="CREATE DATABASE IF NOT EXISTS {$data['database']}";
            if(!mysql_query($sql))
            {
                $this->error('创建数据库失败');
            }

            file_put_contents('config/database.php', "<?php \nreturn ".var_export($data,true).";?>");
            go(U('complete'));
        }
        else
        {
            View::make();
        }
    }

    //完成安装
    public function complete()
    {
        //创建表
        require 'app/Install/data/structure.php';
        //导入数据库
        foreach(glob('app/Install/data/*') as $f)
        {
           if(preg_match('@\d\.php$@', $f))
           {
                require $f;
           }
        }
        file_put_contents('public/lock.php', '');
        View::make();
    }
}









